package com.tarena.tocketmq.controller;

import com.tarena.tocketmq.pojo.po.TeduUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class UserController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/user")
    public String printUser(TeduUser teduUser) {
        log.info("打印用户信息入参：{}", teduUser);
        //包装信息对象
        Message<TeduUser> message = MessageBuilder.withPayload(teduUser).build();
        //使用异步消息发送给listener
        rocketMQTemplate.asyncSend("topic01", message, new SendCallback() {
                    @Override
                    public void onSuccess(SendResult sendResult) {
                        log.info("发送成功：{}", sendResult);
                    }
                    @Override
                    public void onException(Throwable throwable) {
                        log.error("发送失败：{}", throwable.getMessage());
                    }
                }
        );
        return "success";
    }
}
